package com.yantong.controller;

import com.yantong.annotation.Log;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.HeadLine;
import com.yantong.pojo.HeadlinePosition;
import com.yantong.pojo.vo.HeadLineVo;
import com.yantong.service.HeadLineService;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 文章资讯管理控制类
 * @author sujin
 */
@Controller
@RequestMapping("/sys/headLine")
public class HeadLineController extends BaseController {

    private static final String prefix = "system/headLine";
    @Autowired
    private HeadLineService headLineService;

    /***** 文章列表 *****/
    /**
     * 文章页面跳转
     */
    @RequiresPermissions("sys:headLine:headLine")
    @GetMapping("/headLine")
    public String headLine(Model model) {
        Long centreId=null;
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            centreId=getUserCentreId();
        }
        model.addAllAttributes(headLineService.getPositionListToMap(centreId));
        return prefix + "/headLine";
    }

    /**
     * 新增文章页面跳转
     */
    @RequiresPermissions("sys:headLine:addHeadLine")
    @GetMapping("/addHeadLinePage")
    public String addHeadLinePage(Model model) {
        Long centreId=null;
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            centreId=getUserCentreId();
        }
        model.addAllAttributes(headLineService.getPositionListToMap(centreId));
        return prefix + "/addHeadLinePage";
    }

    /**
     * 更新文章页面跳转
     */
    @RequiresPermissions("sys:headLine:editHeadLine")
    @GetMapping("/editHeadLinePage/{id}")
    public String editHeadLinePage(@PathVariable Long id, Model model) {
        Long centreId=null;
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            centreId=getUserCentreId();
        }
        model.addAttribute("position", headLineService.getPositionListToMap(centreId));
        model.addAttribute("headLine", headLineService.getHeadLine(id));
        return prefix + "/editHeadLinePage";
    }

    /**
     * 分页查询文章列表
     */
    @GetMapping("/pageHeadLineList")
    @ResponseBody
    @RequiresPermissions("sys:headLine:headLine")
    public PageUtils<HeadLineVo> pageHeadLineList(HeadLineVo headLineVo, int offset, int limit) {
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            headLineVo.setCentreId(getUserCentreId());
        }
        return headLineService.pageHeadLineList(headLineVo, offset, limit);
    }

    /**
     * 新增文章
     */
    @Log(module = "文章管理", operation = "新增文章")
    @ResponseBody
    @RequiresPermissions("sys:headLine:addHeadLine")
    @PostMapping("/addHeadLine")
    public ResultVo addHeadLine(HeadLine headLine) {
        headLine.setContent(headLine.getContent().replaceAll("<img>",""));
        if (headLine == null) {
            return ResultVo.error();
        }
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            headLine.setCentreId(getUserCentreId());
        }
        headLine.setAddTime(new Date());
        if (headLineService.addHeadLine(headLine) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 更新文章操作
     */
    @Log(module = "文章管理", operation = "修改文章")
    @ResponseBody
    @RequiresPermissions("sys:headLine:editHeadLine")
    @PostMapping("/editHeadLine")
    public ResultVo editHeadLine(HeadLine headLine) {
        headLine.setContent(headLine.getContent().replaceAll("<img>",""));
        if (headLine == null || headLine.getId() == null) {
            return ResultVo.error();
        }
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            headLine.setCentreId(getUserCentreId());
        }
        if (headLineService.updateHeadLine(headLine) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 删除文章
     */
    @Log(module = "文章管理", operation = "删除文章")
    @ResponseBody
    @RequiresPermissions("sys:headLine:removeHeadLine")
    @PostMapping("/removeHeadLine")
    public ResultVo removeHeadLine(Integer id) {
        if (id == null) {
            return ResultVo.error();
        }
        if (headLineService.removeHeadLine(id) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }


    /***** 文章分类 *****/
    /**
     * 文章分类页面跳转
     */
    @RequiresPermissions("sys:headLine:headLinePosition")
    @GetMapping("/headLinePosition")
    public String headLinePosition(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/headLinePosition";
    }

    /**
     * 新增文章分类页面
     */
    @RequiresPermissions("sys:headLine:addHeadLinePosition")
    @GetMapping("/addHeadLinePositionPage")
    public String addHeadLinePositionPage() {
        return prefix + "/addHeadLinePositionPage";
    }

    /**
     * 更新文章分类页面
     */
    @RequiresPermissions("sys:headLine:editHeadLinePosition")
    @GetMapping("/editHeadLinePositionPage/{id}")
    public String editHeadLinePositionPage(@PathVariable Long id, Model model) {
        model.addAttribute("headLinePosition", headLineService.getHeadLinePosition(id));
        return prefix + "/editHeadLinePositionPage";
    }

    /**
     * 文章分类列表
     */
    @GetMapping("/pageHeadLinePositionList")
    @ResponseBody
    @RequiresPermissions("sys:headLine:headLinePosition")
    public PageUtils<HeadlinePosition> pageHeadLinePositionList(String positionName, int offset, int limit) {
        Long centreId=null;
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            centreId=getUserCentreId();
        }
        return headLineService.pageHeadLinePositionList(positionName, centreId, offset, limit);
    }

    /**
     * 新增文章分类
     */
    @Log(module = "文章管理", operation = "新增文章分类")
    @ResponseBody
    @RequiresPermissions("sys:headLine:addHeadLinePosition")
    @PostMapping("/addHeadLinePosition")
    public ResultVo addHeadLinePosition(HeadlinePosition headlinePosition) {
        if (headlinePosition == null) {
            return ResultVo.error();
        }
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            headlinePosition.setCentreId(getUserCentreId());
        }
        if (headLineService.addHeadLinePosition(headlinePosition) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 更新文章分类操作
     */
    @Log(module = "文章管理", operation = "修改文章分类")
    @ResponseBody
    @RequiresPermissions("sys:headLine:editHeadLinePosition")
    @PostMapping("/editHeadLinePosition")
    public ResultVo editHeadLinePosition(HeadlinePosition headlinePosition) {
        if (headlinePosition == null || headlinePosition.getId() == null) {
            return ResultVo.error();
        }
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())){
            headlinePosition.setCentreId(getUserCentreId());
        }
        if (headLineService.updateHeadLinePosition(headlinePosition) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 删除文章分类
     */
    @Log(module = "文章管理", operation = "删除文章分类")
    @ResponseBody
    @RequiresPermissions("sys:headLine:removeHeadLinePosition")
    @PostMapping("/removeHeadLinePosition")
    public ResultVo removeHeadLinePosition(Long id) {
        if (id == null) {
            return ResultVo.error();
        }
        if (headLineService.removeHeadLinePosition(id) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

}
